package com.shioirikukuri.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DiningOrder {

	private Integer diningOrderId;                // 点餐订单ID
	private String dishes;                       // 所点菜品（JSON格式存储）[9](@ref)
	private String combos;                       // 所点套餐（JSON格式存储）[9](@ref)
	private Double baseTotal;                    // 点餐总价[9](@ref)
	private Double priceAdjustment;              // 特殊价格浮动比例（%）[9](@ref) 百分比浮动，可以是正负数
	private Long couponId;                        //普通预约使用的优惠卷类型ID,若为其他类型预约，则强制为null
	private Double finalTotal;                   // 最终点餐总价[9](@ref)
	private LocalDateTime createTime;            // 创建时间[9](@ref)

	public void calculateFinalTotal() {
		if (baseTotal == null) baseTotal = 0.0;
		if (priceAdjustment == null) priceAdjustment = 0.0;

		BigDecimal base = new BigDecimal(Double.toString(baseTotal));
		BigDecimal adjustment = new BigDecimal(Double.toString(priceAdjustment));
		BigDecimal hundred = new BigDecimal("100");

		// 计算：finalTotal = baseTotal * (1 + priceAdjustment / 100)
		BigDecimal multiplier = adjustment.divide(hundred, 4, RoundingMode.HALF_UP)
				.add(BigDecimal.ONE);

		BigDecimal finalAmount = base.multiply(multiplier).setScale(2, RoundingMode.HALF_UP);

		this.finalTotal = finalAmount.doubleValue();
	}
}
